<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Members extends CI_Controller {

	public function __construct(){
		parent::__construct();

		// load helper
		$this->load->helper('url');
		$this->load->library('session');

		/* load model */
		$this->load->model("State", '', TRUE);
		$this->load->model("Country", '', TRUE);
		$this->load->model("Authentication", '', TRUE);
		$this->load->model("Member", '', TRUE);
		$this->load->model("Fraud", '', TRUE);
	}	

	public function index(){
		
		if (!$this->Authentication->IsAuthenticated_Session()){
			redirect('/members/login');
		}
		
		$data['mainView'] = 'index';
        $data['controllerName'] = 'members';
        $this->load->view('layouts/layout', $data);
	}

	public function register($ref=1){

        $data['mainView'] = 'register';
        $data['controllerName'] = 'members';

        $data['states'] = $this->State->getAllState();
        $data['countries'] = $this->Country->getAllCountry();

        $this->session->set_userdata('ref', $ref);

        $this->load->view('layouts/layout', $data);
	}

	public function register_post(){

		if( $this->Fraud->isEnable(FRAUD_ID_IP) && !$this->Fraud->checkIP() )
			redirect('/members/register_error/'.FRAUD_ID_IP);

		else if( $this->Fraud->isEnable(FRAUD_ID_USERNAME) && !$this->Fraud->checkUsername() )
			redirect('/members/register_error/'.FRAUD_ID_USERNAME);

		else if( $this->Fraud->isEnable(FRAUD_ID_EMAIL) && !$this->Fraud->checkEmail() )
			redirect('/members/register_error/'.FRAUD_ID_EMAIL);	

  		$this->Member->add($this->session->userdata('ref'));
  		redirect('/members/index');
	}


	public function register_error($fraudId){

		$data['error_message'] = $this->Fraud->getFraudMessage($fraudId);

        $data['mainView'] = 'register_error';
        $data['controllerName'] = 'members';

        $data['states'] = $this->State->getAllState();
        $data['countries'] = $this->Country->getAllCountry();

        $this->load->view('layouts/layout', $data);
	}

	public function login(){

        $data['mainView'] = 'login';
        $data['controllerName'] = 'members';
		$this->load->view('layouts/layout', $data);
	}

	public function login_post(){

		if($this->Authentication->IsAuthenticated())
			redirect('/members/index');
		else
			redirect('/members/login_error');
	}

	public function login_error(){

        $data['mainView'] = 'login_error';
        $data['controllerName'] = 'members';
		$this->load->view('layouts/layout', $data);
	}

	public function logout(){

		$this->Authentication->Logout();
		redirect('/home/index');
  	}


	public function inbox(){

		if (!$this->Authentication->IsAuthenticated_Session()){
			redirect('/members/login');
		}

		$data['mainView'] = 'inbox';
        $data['controllerName'] = 'members';
        $this->load->view('layouts/layout', $data);
  	}


	public function balance(){

		if (!$this->Authentication->IsAuthenticated_Session()){
			redirect('/members/login');
		}

		$data['mainView'] = 'balance';
        $data['controllerName'] = 'members';
        $this->load->view('layouts/layout', $data);
  	}


	public function points(){

		if (!$this->Authentication->IsAuthenticated_Session()){
			redirect('/members/login');
		}

		$data['mainView'] = 'points';
        $data['controllerName'] = 'members';
        $this->load->view('layouts/layout', $data);
  	}

	public function cash(){

		if (!$this->Authentication->IsAuthenticated_Session()){
			redirect('/members/login');
		}

		$data['mainView'] = 'cash';
        $data['controllerName'] = 'members';
        $this->load->view('layouts/layout', $data);
  	}

	public function test()	{
		$this->load->view('members/test');
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */